import React from 'react';
import { InputNumber, Form } from 'antd';
import _ from 'lodash';

export default class InputEditor extends React.PureComponent {
  state = {};

  onChange = value => {
    if (!_.isNumber(value)) {
      return;
    }
    let result = value;
    const min = this.props.min ? this.props.min : 0;
    if (min >= 0 && _.toNumber(value) < 0) {
      result = min;
    }
    this.props.onChange(result);
  };

  render() {
    const { cellData } = this.props;
    let value = null;

    if (cellData !== null) {
      if (_.isArray(cellData)) {
        // eslint-disable-next-line prefer-destructuring
        value = cellData[0];
      } else {
        value = cellData;
      }
    }

    return (
      <Form.Item
        hasFeedback
        validateStatus={this.props.validateStatus}
        style={{ width: '100%', padding: 0, margin: 0 }}
      >
        <InputNumber
          style={{ width: '100%' }}
          precision={0}
          min={0}
          step={100.0}
          value={value}
          {...this.props}
          onChange={this.onChange}
        />
      </Form.Item>
    );
  }
}
